<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="日期" prop="dataDate">
                <el-date-picker clearable v-model="queryParams.dataDate" type="date" value-format="yyyy-MM-dd"
                    placeholder="请选择日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="责任人" prop="responsiblePerson">
                <!-- <el-input v-model.trim="queryParams.responsiblePerson" placeholder="请输入责任人" clearable
                    @keyup.enter.native="handleQuery" /> -->
                <el-select v-model="queryParams.responsiblePerson" clearable placeholder="原始责任人" filterable>
                    <el-option v-for="(val, key) in userList" :key="key" :label="val" :value="val" />
                </el-select>
            </el-form-item>
            <!-- <el-form-item label="期初数量" prop="initialQuantity">
                <el-input v-model.trim="queryParams.initialQuantity" placeholder="请输入期初数量" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="发货数量" prop="shipmentQuantity">
                <el-input v-model.trim="queryParams.shipmentQuantity" placeholder="请输入发货数量" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="退回数量" prop="returnedQuantity">
                <el-input v-model.trim="queryParams.returnedQuantity" placeholder="请输入退回数量" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="期末数量" prop="endQuantity">
                <el-input v-model.trim="queryParams.endQuantity" placeholder="请输入期末数量" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="逻辑删除" prop="isDelete">
                <el-input v-model.trim="queryParams.isDelete" placeholder="请输入逻辑删除" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item> -->
            <el-form-item label="部门" prop="appDept">
                <el-input v-model.trim="queryParams.appDept" placeholder="请输入部门" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['product:BiShipmentsRecord:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['product:BiShipmentsRecord:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['product:BiShipmentsRecord:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['product:BiShipmentsRecord:export']">导出</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button size="mini" @click="openType = true">初始化</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-dialog :visible.sync="openType" width="700px" append-to-body v-if="openType" title="请选择时间段">
            <commonDay @getDateArr="getcommonDay" @changDay="changcommonDay" :timeArr="timeArrList"
                :dayActive="this.dayActive" :dayLimit="1095" />
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleRefreshCache">初始化</el-button>
                <!-- <el-button @click="cancel">取 消</el-button> -->
            </div>
        </el-dialog>
        <el-table v-loading="loading" :data="BiShipmentsRecordList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="id" align="center" prop="id" />
            <el-table-column label="日期" align="center" prop="dataDate" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.dataDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="责任人" align="center" prop="responsiblePerson" />
            <el-table-column label="期初数量" align="center" prop="initialQuantity" />
            <el-table-column label="发货数量" align="center" prop="shipmentQuantity" />
            <el-table-column label="退回数量" align="center" prop="returnedQuantity" />
            <el-table-column label="期末数量" align="center" prop="endQuantity" />
            <el-table-column label="逻辑删除" align="center" prop="isDelete" />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column label="部门" align="center" prop="appDept" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['product:BiShipmentsRecord:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['product:BiShipmentsRecord:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改进销存期初期末记录对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="日期" prop="dataDate">
                    <el-date-picker clearable v-model="form.dataDate" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="责任人" prop="responsiblePerson">
                    <el-input v-model.trim="form.responsiblePerson" placeholder="请输入责任人" />
                </el-form-item>
                <el-form-item label="期初数量" prop="initialQuantity">
                    <el-input v-model.trim="form.initialQuantity" placeholder="请输入期初数量" />
                </el-form-item>
                <el-form-item label="发货数量" prop="shipmentQuantity">
                    <el-input v-model.trim="form.shipmentQuantity" placeholder="请输入发货数量" />
                </el-form-item>
                <el-form-item label="退回数量" prop="returnedQuantity">
                    <el-input v-model.trim="form.returnedQuantity" placeholder="请输入退回数量" />
                </el-form-item>
                <el-form-item label="期末数量" prop="endQuantity">
                    <el-input v-model.trim="form.endQuantity" placeholder="请输入期末数量" />
                </el-form-item>
                <el-form-item label="逻辑删除" prop="isDelete">
                    <el-input v-model.trim="form.isDelete" placeholder="请输入逻辑删除" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model.trim="form.remark" placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="部门" prop="appDept">
                    <el-input v-model.trim="form.appDept" placeholder="请输入部门" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listSampleApply,
    getSampleApply,
    delSampleApply,
    addSampleApply,
    updateSampleApply,
    addApply,
    returnApply,
    selectSampleTalentApplyToDo,
    SampleTalentApplyCheck,
    startApplyFlow,
    selectFlowInstanceRecordListExt,
    selectMySampleTalentApply,
    uploadIst,
    deptlistSampleApply,
    alllistSampleApply,
    checkInventory,
    checkDingdingStatus,
    startApplyExcel,
    checkAfterSaleNoInfoReq,
    checkPackageStatus,
    batchEditIsReturn,
    isValidData,
    selectuserList,
    transferResponsiblePeople
} from "@/api/sample/sampleApply";
import { initData, listBiShipmentsRecord, getBiShipmentsRecord, delBiShipmentsRecord, addBiShipmentsRecord, updateBiShipmentsRecord } from "@/api/sample/BiShipmentsRecord";
import commonDay from "@/common/day/index.vue";
export default {
    name: "BiShipmentsRecord",
    components: { commonDay },
    data() {
        return {
            openType: false,
            dayActive: 365,
            timeArrList: [
                // { value: "2", label: "昨天" },
                { value: "7", label: "最近7天" },
                { value: "30", label: "最近30天" },
                { value: "365", label: "最近一年" },
                { value: "1", label: "日" },
            ],
            timeGap: { startTime: "", endTime: "" },
            userList: [],
            inputValue: '',
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 进销存期初期末记录表格数据
            BiShipmentsRecordList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                dataDate: null,
                responsiblePerson: null,
                initialQuantity: null,
                shipmentQuantity: null,
                returnedQuantity: null,
                endQuantity: null,
                isDelete: null,
                appDept: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            }
        };
    },
    created() {
        selectuserList().then((response) => {
            this.userList = response
        });
        this.getList();
    },
    methods: {
        // 打开加载层
        openLoading() {
            this.$modal.loading("正在加载，请稍候！");
        },
        /** 刷新缓存按钮操作 Warning*/
        handleRefreshCache() {
            // this.$modal.msgWarning("数据正在同步，请稍候");
            const obj = {
                startDate: this.timeGap.startTime,
                endDate: this.timeGap.endTime,
            };
            this.openLoading();
            initData(obj).then((response) => {
                this.$modal.closeLoading();

                if (response.code == 200) {
                    this.$modal.msgSuccess(response.msg);
                    this.dayActive = 365;
                    this.queryParams.startDate = null;
                    this.queryParams.endDate = null;
                    // this.openauditResult = false;
                    this.openType = false;
                    this.getList();
                }
                // this.$store.dispatch('dict/cleanDict');
            });
        },
        //时间控件
        changcommonDay(val) {
            this.dayActive = val;
            this.timeGap.startTime = val[0];
            this.timeGap.endTime = val[1];
        },
        getcommonDay(value) {
            this.timeGap.startTime = value[0];
            this.timeGap.endTime = value[1];
            console.log(JSON.stringify(this.timeGap));

            // this.queryParams.startDate = this.timeGap.startTime;
            // this.queryParams.endDate = this.timeGap.endTime;

            // this.getList();

            this.dayActive = 1;
        },
        /** 查询进销存期初期末记录列表 */
        getList() {
            this.loading = true;
            listBiShipmentsRecord(this.queryParams).then(response => {
                this.BiShipmentsRecordList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                dataDate: null,
                responsiblePerson: null,
                initialQuantity: null,
                shipmentQuantity: null,
                returnedQuantity: null,
                endQuantity: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                isDelete: null,
                remark: null,
                appDept: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加进销存期初期末记录";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getBiShipmentsRecord(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改进销存期初期末记录";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateBiShipmentsRecord(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addBiShipmentsRecord(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除进销存期初期末记录编号为"' + ids + '"的数据项？').then(function () {
                return delBiShipmentsRecord(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('product/BiShipmentsRecord/export', {
                ...this.queryParams
            }, `BiShipmentsRecord_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
